﻿using System;
using System.Web.Mvc;
using mvcForum.AddOns.ViewModels;
using mvcForum.Core.Abstractions.Interfaces;
using mvcForum.Core.Interfaces.AddOns;
using mvcForum.Core.Interfaces.Data;
using mvcForum.Web.Controllers;
using mvcForum.Web.Helpers;
using mvcForum.Web.Interfaces;
using mvcForum.AddOns.MultiPost;

namespace mvcForum.AddOns.Controllers {

	public class MultiPostConfigurationController : ForumAdminBaseController, IAntiSpamConfigurationController {
		private readonly MultiPostConfiguration config;

		public MultiPostConfigurationController(IWebUserProvider userProvider, IContext context , IAddOnConfiguration<MultiPostAddOn> config)
			: base(userProvider, context) {

			this.config = (MultiPostConfiguration)config;
		}

		public ViewResult Index() {
			return View(new MultiPostViewModel { Enabled = this.config.Enabled, Interval = this.config.Interval, Posts = this.config.Posts, RunAsynchronously = this.config.RunAsynchronously, Delay = this.config.Delay });
		}

		[HttpPost]
		public ViewResult Index(MultiPostViewModel model) {
			if (ModelState.IsValid) {
				this.config.Enabled = model.Enabled;
				if (model.Interval.HasValue) {
					this.config.Interval = model.Interval.Value;
				}
				if (model.Posts.HasValue) {
					this.config.Posts = model.Posts.Value;
				}
				this.config.Delay = model.Delay;

				this.Context.SaveChanges();

				TempData["Saved"] = true;
			}

			return View(model);
		}

		public string Name {
			get {
				return ForumHelper.GetString("Name", null, "mvcForum.AddOns.MultiPost");
			}
		}

		public string Description {
			get {
				return ForumHelper.GetString("Description", null, "mvcForum.AddOns.MultiPost");
			}
		}
	}
}